/*
 * Date          Author       Note
 * 2015.05.28    wzwz         进度条
 */




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "com.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "mhstatic.h"
#include "keypane.h"
#include "progress.h"
#include "carriage.h"
#include <assert.h>
#include "progress_all.h"

#define PERCENT_NUM       100
/*
 *
 */
static void CreateProgressAllInterface(HWND hWnd)
{
     HWND hTmp;
    CreateWindow(MH_LABEL_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_TIP,
        235,
        60,
        40,
        30,
        hWnd, MH_LABEL_BKGND_3GREEN | MH_LABEL_TEXT_GREEN);
    hTmp = CreateWindow(MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        103,
        100,
        CX_DAT_ENTER_BMP,
        CY_DAT_ENTER_BMP,
        hWnd, (int)&g_bmp[BMP_DAT_ESC]);
    ShowWindow(hTmp, SW_HIDE);

}
/*
 *
 */
static void* Update(void *arg)
{
    HWND hWnd = (HWND)arg;
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    uchar percet = 0;
   if(data.pwnd_update)
    {
        while(1)
        {
            GetProgressData(g_knit_data, &percet, data.pwnd_update->file);
            if(percet != data.pwnd_update->progress)
            {
                if(percet <= PERCENT_NUM)
                {
                    data.pwnd_update->progress = percet;
                    HWND hTmp = GetDlgItem(hWnd, IDC_TIP);
                    char sc[32] = {0};
                    sprintf(sc, "%d%%\n", percet);
                    SetWindowCaption(hTmp, sc);
                }
                if(percet >= PERCENT_NUM)
                {
                    data.pwnd_update->progress = PERCENT_NUM;
                    break;
                }
            }
        }
    }
    pthread_exit(0);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    switch(nID)
    {
    case ID_ESC:
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    switch(key)
    {
    case SCANCODE_ESCAPE:
        OnBtnCmd(hWnd, ID_ESC);
        break;
    }
}
/*
 *
 */
static int ProgressAllWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateProgressAllInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.top += MASTER_TITLE_TOP;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;

            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));

            DrawText(hdc, GetWindowCaption(hWnd), -1, &rect, DT_CENTER
                    | DT_VCENTER | DT_SINGLELINE);

            ReleaseDC(hdc);
        }
        break;
      case MSG_UPDATE:
        {
            WND_EXTRA_DATA data;
            static char sc[MH_FILE_NAME_MAX_LEN] = {0};
            data.value = GetWindowAdditionalData(hWnd);
            usleep(1);  //目的是为了先让线程获取到数据
            SetWindowCaption(hWnd, data.pwnd_update->file);
            if(memcmp(sc, data.pwnd_update->file, MH_FILE_NAME_MAX_LEN))
            {
                memcpy(sc, data.pwnd_update->file, MH_FILE_NAME_MAX_LEN);
                SendMessage(hWnd, MSG_ERASEBKGND, 0, 0);
                SendMessage(hWnd, MSG_PAINT, 0, 0);
            }
            HWND hTmp = GetDlgItem(hWnd, IDC_TIP);
            SendMessage(hTmp, MSG_ERASEBKGND, 0, 0);
            SendMessage(hTmp, MSG_PAINT, 0, 0);

            HDC hdc = GetClientDC(hWnd);
            int width = 198;
            int mid = data.pwnd_update->progress * width / PERCENT_NUM;
            if(mid)
            {
                SetBrushColor(hdc, COLOR_HQ_YELLOW(hdc));
                FillBox(hdc, 21, 60, mid, 30);
            }
            Rectangle(hdc, 20, 60, 219, 89);
            ReleaseDC(hdc);


            if(data.pwnd_update->progress == PERCENT_NUM)
            {
                hTmp = GetDlgItem(hWnd, ID_ESC);
                ShowWindow(hTmp, SW_SHOWNORMAL);
            }
        }
        return 0;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_update)
            {
                FREE(data.pwnd_update);
                SetWindowAdditionalData(hWnd, 0);
            }
            pthread_join(id, NULL);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            HDC hdc = GetClientDC(hWnd);
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_ASK_DLG]);
            ReleaseDC(hdc);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateProgressAllWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_ASK_DLG_BMP) / 2;
    int y = (CY_SCREEN - CY_ASK_DLG_BMP) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(250);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = ProgressAllWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_ASK_DLG_BMP;
    CreateInfo.by = y + CY_ASK_DLG_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    PWND_UPDATE p = calloc(1, sizeof(WND_UPDATE));
    CreateInfo.dwAddData = (int)p;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_ESC));
        ShowWindow(hWnd, SW_SHOWNORMAL);
        pthread_create(&id, NULL, Update, (void*)hWnd);
    }
    return hWnd;
}
